<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="关键词id">
      </el-table-column>
      <el-table-column
        prop="educationName"
        header-align="center"
        align="center"
        label="五育">
      </el-table-column>
      <el-table-column
        prop="eduthemeName"
        header-align="center"
        align="center"
        label="五育大主题">
      </el-table-column>
      <el-table-column
        prop="eduaspectName"
        header-align="center"
        align="center"
        label="大主题对应方面">
      </el-table-column>
      <el-table-column
        prop="keys"
        header-align="center"
        align="center"
        label="关键词">
      </el-table-column>
      <el-table-column
        prop="keysLevel"
        header-align="center"
        align="center"
        label="关键词等级">
      </el-table-column>
      <el-table-column
        prop="studentType"
        header-align="center"
        align="center"
        label="学生类型">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>
      <el-table-column
        prop="updateBy"
        header-align="center"
        align="center"
        label="更新者">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateNewKeysStatus(scope.row.id,1)">通过</el-button>
          <el-button type="text" size="small" @click="updateNewKeys(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="updateNewKeysStatus(scope.row.id,-1)">不通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->


    <div>
      <el-dialog
        title="修改"
        :visible.sync="updateVisible"
        width="30%"
      >

        <el-form title="" ref="form" :model="dataForm" label-width="80px">
              <el-form-item label="所属五育">
                <el-select @change="changeEdutheme" v-model="dataForm.type" placeholder="请选择所属五育">
                  <el-option label="德育" value="德育"></el-option>
                  <el-option label="智育" value="智育"></el-option>
                  <el-option label="体育" value="体育"></el-option>
                  <el-option label="美育" value="美育"></el-option>
                  <el-option label="劳育" value="劳育"></el-option>
                </el-select>
          </el-form-item>

          <el-form-item label="所属主题">
            <el-select  @change="dataForm.aspect=''"  v-model="dataForm.edutheme" placeholder="请选择所属主题">
              <el-option v-for="(value,key) in keysMap[dataForm.type]" :key="key" :label="key" :value="key"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="所属层面">
            <el-select v-model="dataForm.aspect" placeholder="请选择所属层面">
              <template v-if="dataForm.edutheme!==''">
                <el-option v-for="(value,key) in keysMap[dataForm.type][dataForm.edutheme]" :key="key" :label="key"
                           :value="key"></el-option>
              </template>

            </el-select>
          </el-form-item>

          <el-form-item label="等级">
            <el-select v-model="dataForm.keysLevel" placeholder="等级">
              <el-option label="优秀" value="优秀"></el-option>
              <el-option label="中等" value="中等"></el-option>
              <el-option label="偏差" value="偏差"></el-option>
              <el-option label="无" value="无"></el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateKey">确 定</el-button>
      </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import AddOrUpdate from './eascommentkeys-add-or-update'

export default {
  data () {
    return {
      dataForm: {
        key: '',
        type: '',
        aspect: '',
        edutheme: '',
        keysLevel: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      updateVisible: false,
      keysMap: {}
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/eas/eascommentkeys/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key,
          showName: true,
          type: 1,
          status: 0
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/eas/eascommentkeys/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    updateNewKeysStatus (id, status) {
      const data = {
        id,
        status
      }
      this.$http({
        url: this.$http.adornUrl('/eas/eascommentkeys/updateStatusCustomKeys'),
        method: 'get',
        params: this.$http.adornParams(data, false)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    updateNewKeys (row) {
      this.updateVisible = true
      this.dataForm = {
        id: row.id,
        type: row.educationName,
        edutheme: row.eduthemeName,
        aspect: row.eduaspectName,
        keysLevel: row.keysLevel
      }
    },
    updateKey () {
      this.$http({
        url: this.$http.adornUrl('/eas/eascommentkeys/updateByAspect'),
        method: 'get',
        params: this.$http.adornParams({
          id: this.dataForm.id,
          aspect: this.dataForm.aspect,
          keysLevel: this.dataForm.keysLevel
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        } else {
          this.$message.error(data.msg)
        }
        this.updateVisible = false
      })
    },
    changeEdutheme () {
      this.dataForm.edutheme = ''
      this.dataForm.aspect = ''
    }
  },
  mounted () {
    this.$http({
      url: this.$http.adornUrl('/eas/eascommentkeys/keys'),
      method: 'get'
    }).then(({data}) => {
      if (data.code === 0) {
        this.keysMap = data.keys
      } else {
        this.$message.error(data.msg)
      }
    })
  }
}
</script>
